package ext.acae2.dms.parse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import ext.acae2.dms.bean.EPMBuildRuleBean;
import ext.acae2.dms.bean.EPMDocumentBean;
import ext.acae2.dms.bean.EPMReferenceLinkBean;
import ext.acae2.dms.bean.PartBean;
import ext.acae2.util.ParameterUtil;

/**
 * @desription parse all link
 * @author cnie
 * @date 2013-4-25 下午1:12:52
 */
public class ParseLink {
	
	
	private static final String DRAWING_TYPE_LIST="UGPART,AutoCAD,ACAD";
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void parseLink(Map<String,List> dataMap){
		
		List<EPMBuildRuleBean> epmBuildRuleList = new ArrayList<EPMBuildRuleBean>();
		
		List<EPMReferenceLinkBean> refLinkList = new ArrayList<EPMReferenceLinkBean>();
		
		List<EPMDocumentBean> epmDocList = dataMap.get(ParameterUtil.EPMDOCUMENTBEAN);
		List<PartBean> partBeanList = dataMap.get(ParameterUtil.PARTBEAN);
		
		for(Iterator<EPMDocumentBean> epmDocIt =epmDocList.iterator();epmDocIt.hasNext();){
			EPMDocumentBean epmDocBean = epmDocIt.next();
			String partIds = epmDocBean.getPartId();
			String epmType = epmDocBean.getType();
			if(StringUtils.isNotBlank(partIds)){
				String[] partIdArr = partIds.split(",");
				List<String> partIdList = Arrays.asList(partIdArr);
				for(Iterator<PartBean> partBeanIt = partBeanList.iterator();partBeanIt.hasNext();){
					PartBean partBean = partBeanIt.next();
					String partId = partBean.getId();
					if(partIdList.contains(partId)){
						
						epmDocBean.setVersion(
								partBean.getVersion() + "."
										+ epmDocBean.getVersion());
						
						partBean.setNumber(partBean.getMap().get("drawingNo"));
						epmDocBean.setMap(partBean.getMap());
						if("prt".equalsIgnoreCase(epmDocBean.getFormat()))
							epmDocBean.setNumber(epmDocBean.getName());
						else{
							epmDocBean.setNumber(epmDocBean.getName().substring(0,epmDocBean.getName().lastIndexOf("."))+"("+partBean.getNumber()+")"+epmDocBean.getFormat());
							epmDocBean.setName(epmDocBean.getName().substring(0,epmDocBean.getName().lastIndexOf("."))+"("+partBean.getNumber()+")"+epmDocBean.getFormat());
						}
						
						
						/**modify 20130624 by cnie begin **/
						/*epmDocBean.setNumber(epmDocBean.getName()+"("+ partBean.getNumber() +")");
						epmDocBean.setName(epmDocBean.getName()+"("+ partBean.getNumber() +")");
						
						
						epmDocBean.setOBJNAME(
								partBean.getOBJNAME());
						epmDocBean.setDRAWINGNO(
								partBean.getDRAWINGNO());
						epmDocBean.setCOPYRIGHT(
								partBean.getCOPYRIGHT());
						epmDocBean.setCHARACTERTYPE(partBean.getCHARACTERTYPE());
						epmDocBean.setMASS(
								partBean.getMASS());
						epmDocBean.setUNITE(
								partBean.getUNITE());
						epmDocBean.setMODEL(
								partBean.getMODEL());
						epmDocBean.setDESIGN(
								partBean.getDESIGN());
						
						epmDocBean.setSECRETLEVEL(
								partBean.getSECRETLEVEL());
						epmDocBean.setSECURITYDEADLINE(
								partBean.getSECURITYDEADLINE());
						
						epmDocBean.setPARTTYPE(
								partBean.getPARTTYPE());
						epmDocBean.setBLANKSPEC(
								partBean.getBLANKSPEC());
						epmDocBean.setBLANKSTATE(
								partBean.getBLANKSTATE());
						epmDocBean.setMATERIAL(
								partBean.getMATERIAL());
						epmDocBean.setMATERIALSTANDARD(
								partBean.getMATERIALSTANDARD());
						epmDocBean.setCOPYRIGHT(
								partBean.getCOPYRIGHT());*/
						/**modify 20130624 by cnie end **/
						if(DRAWING_TYPE_LIST.indexOf(epmType)<0){
							EPMBuildRuleBean epmBuildRule = new EPMBuildRuleBean(partBean,epmDocBean);
							epmBuildRuleList.add(epmBuildRule);
						}else{
							
							/**********deal 2D and 3D link*****************/
							boolean refFlag = false;
							for(EPMDocumentBean comEpmBean:epmDocList){
								if(DRAWING_TYPE_LIST.indexOf(comEpmBean.getType())<0){  //is 3D prt
									String comPartIds = comEpmBean.getPartId();
									List<String> comPartIdList = Arrays.asList(comPartIds.split(","));
									if(comPartIdList.contains(partId)){
										refFlag = true;
										EPMReferenceLinkBean refLinkBean = new EPMReferenceLinkBean();
										refLinkBean.setEpmBean(epmDocBean);  //CADDrawing
										refLinkBean.setRefEpmBean(comEpmBean); //CADCompontent
										refLinkList.add(refLinkBean);
									}
								}
							}
							
							if(!refFlag){
								/************part is only contains drawing CAD  ***************/
							}
							
						}
						
					}
				}
			}
		}
		
		
		System.out.println("epmBuildRuleList=============>"+epmBuildRuleList.size());
		
		dataMap.put(ParameterUtil.EPMBUILDRULELINK,epmBuildRuleList);
		dataMap.put(ParameterUtil.EPMBUILDHISTORYLINK, epmBuildRuleList);
		dataMap.put(ParameterUtil.EPMREFERENCELINK, refLinkList);
		
		ParsePartUsageLink.excute(dataMap);
		
		
	}

}
